﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __42084F7C_88EE_4c32_B73C_0331C2D6EAC0__
#define __42084F7C_88EE_4c32_B73C_0331C2D6EAC0__

#include "../tools/head.h"

//////////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_box
{

    class i_variant;

    class property_impl : public xos_stl::mem_item< xos::release_impl< property_impl >, thread_lock >,
		public i_property
    {
    public:
        typedef std::map< std::string, i_variant* > VT_NAME_MAP;
        typedef VT_NAME_MAP::iterator VT_NAME_ITER;

        typedef std::map< int, i_variant* > VT_ID_MAP;
        typedef VT_ID_MAP::iterator VT_ID_ITER;

        typedef xos::release_impl< property_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //

        static int get_addr( POOL *** pppPool, LIST *** pppList );

    public:
        property_impl();
        ~property_impl();

    protected:
        VT_NAME_ITER m_name_iter;
        VT_NAME_MAP m_name_map;

        VT_ID_ITER m_id_iter;
        VT_ID_MAP m_id_map;

    public:
        int init();
        int term();

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        // name index
        // 

        const char * str( const char * lpszName, char * lpszValue, int nBufSize, int * lpnSizeNeeded );
        xos::i_release * obj( const char * lpszName, xos::i_release ** ppv );
        xos_box::i_list * list( const char * lpszName, xos_box::i_list ** ppv );
        i_property * prop( const char * lpszName, i_property ** ppv );
        v_create fun( const char * lpszName, v_create * pv );
        double dbl( const char * lpszName, double * lpdValue );
        float flt( const char * lpszName, float * lpfValue );
        int it( const char * lpszName, int  * lpnValue );

        xos::xos_u64 * u64_ptr( const char * lpszName, xos::xos_u64 ** lppRet );
        xos::xos_i64 * i64_ptr( const char * lpszName, xos::xos_i64 ** lppRet );
        xos::xos_u32 * u32_ptr( const char * lpszName, xos::xos_u32 ** lppRet );
        xos::xos_i32 * i32_ptr( const char * lpszName, xos::xos_i32 ** lppRet );
        xos::xos_u16 * u16_ptr( const char * lpszName, xos::xos_u16 ** lppRet );
        xos::xos_i16 * i16_ptr( const char * lpszName, xos::xos_i16 ** lppRet );
        xos::xos_u8 * u8_ptr( const char * lpszName, xos::xos_u8 ** lppRet );
        xos::xos_i8 * i8_ptr( const char * lpszName, xos::xos_i8 ** lppRet );
        char * char_array( const char * lpszName, char ** lppRet, int * lpnSize, int * lpnLen );
        char * bin_ptr( const char * lpszName, char ** lppRet, int * lpnSize, int * lpnLen );
        double * dbl_ptr( const char * lpszName, double ** lppRet );
        float * flt_ptr( const char * lpszName, float ** lppRet );
        bool * bool_ptr( const char * lpszName, bool ** lppRet );

        int set( const char * lpszName, const char * lpszValue );
        int set_obj( const char * lpszName, xos::i_release * pv, bool bAutoRelease );
        int set_list( const char * lpszName, xos_box::i_list * pv, bool bAutoRelease );
        int set_prop( const char * lpszName, i_property * pv, bool bAutoRelease );
        int set( const char * lpszName, i_variant * pv );
        int set( const char * lpszName, v_create pv );
        int set( const char * lpszName, double value );
        int set( const char * lpszName, float value );
        int set( const char * lpszName, int value );

        int set( const char * lpszName, xos::xos_u64 * lpRef );
        int set( const char * lpszName, xos::xos_i64 * lpRef );
        int set( const char * lpszName, xos::xos_u32 * lpRef );
        int set( const char * lpszName, xos::xos_i32 * lpRef );
        int set( const char * lpszName, xos::xos_u16 * lpRef );
        int set( const char * lpszName, xos::xos_i16 * lpRef );
        int set_u8( const char * lpszName, xos::xos_u8 * lpRef );
        int set_i8( const char * lpszName, xos::xos_i8 * lpRef );
        int set_char_array( const char * lpszName, char * lpRef, int nBufSize );
        int set_bin( const char * lpszName, char * lpRef, int nBufSize );
        int set( const char * lpszName, double * lpRef );
        int set( const char * lpszName, float * lpRef );
        int set( const char * lpszName, bool * lpRef );

        i_variant * remove( const char * lpszName );
        i_variant * vt( const char * lpszName );

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        // id index
        // 

        const char * str( int nId, char * lpszValue, int nBufSize, int * lpnSizeNeeded );
        xos::i_release * obj( int nId, xos::i_release ** ppv );
        xos_box::i_list * list( int nId, xos_box::i_list ** ppv );
        i_property * prop( int nId, i_property ** ppv );
        v_create fun( int nId, v_create * pv );
        double dbl( int nId, double * lpdValue );
        float flt( int nId, float * lpfValue );
        int it( int nId, int  * lpnValue );

        xos::xos_u64 * u64_ptr( int nId, xos::xos_u64 ** lppRet );
        xos::xos_i64 * i64_ptr( int nId, xos::xos_i64 ** lppRet );
        xos::xos_u32 * u32_ptr( int nId, xos::xos_u32 ** lppRet );
        xos::xos_i32 * i32_ptr( int nId, xos::xos_i32 ** lppRet );
        xos::xos_u16 * u16_ptr( int nId, xos::xos_u16 ** lppRet );
        xos::xos_i16 * i16_ptr( int nId, xos::xos_i16 ** lppRet );
        xos::xos_u8 * u8_ptr( int nId, xos::xos_u8 ** lppRet );
        xos::xos_i8 * i8_ptr( int nId, xos::xos_i8 ** lppRet );
        char * char_array( int nId, char ** lppRet, int * lpnSize, int * lpnLen );
        char * bin_ptr( int nId, char ** lppRet, int * lpnSize, int * lpnLen );
        double * dbl_ptr( int nId, double ** lppRet );
        float * flt_ptr( int nId, float ** lppRet );
        bool * bool_ptr( int nId, bool ** lppRet );

        int set( int nId, const char * lpszValue );
        int set_obj( int nId, xos::i_release * pv, bool bAutoRelease );
        int set_list( int nId, xos_box::i_list * pv, bool bAutoRelease );
        int set_prop( int nId, i_property * pv, bool bAutoRelease );
        int set( int nId, i_variant * pv );
        int set( int nId, v_create pv );
        int set( int nId, double value );
        int set( int nId, float value );
        int set( int nId, int value );

        int set( int nId, xos::xos_u64 * lpRef );
        int set( int nId, xos::xos_i64 * lpRef );
        int set( int nId, xos::xos_u32 * lpRef );
        int set( int nId, xos::xos_i32 * lpRef );
        int set( int nId, xos::xos_u16 * lpRef );
        int set( int nId, xos::xos_i16 * lpRef );
        int set_u8( int nId, xos::xos_u8 * lpRef );
        int set_i8( int nId, xos::xos_i8 * lpRef );
        int set_char_array( int nId, char * lpRef, int nBufSize );
        int set_bin( int nId, char * lpRef, int nBufSize );
        int set( int nId, double * lpRef );
        int set( int nId, float * lpRef );
        int set( int nId, bool * lpRef );

        i_variant * remove( int nId );
        i_variant * vt( int nId );

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        // other helper function
        // 

        i_variant * name_data( const char ** ppKey, char * lpszKey, int nBufSize, int * lpnSizeNeeded );
        i_variant * id_data( int * pnId );
        int name_next();
        int id_next();
        int reset();

        int size( int * pNamePropSize, int * pIdPropSize );
        int release_all();

    protected:
        i_variant * get( const char * lpszName, bool bCreate );
        i_variant * get( int nId, bool bCreate );
        int init_data();

    };

} // so_common

#endif // __42084F7C_88EE_4c32_B73C_0331C2D6EAC0__
